/*
 * @Description: mark
 * @Author: wangwangwang
 * @Date: 2020-06-28 18:18:01
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-30 10:06:12
 */
import { getPermission, login2, wechatLogin } from '@/api/api';
import { getSystemDetails } from '@/api/common';
import { getToken, getUserId, removePerMission, removeToken, setPerMission, setToken, setUserId, setUserInfo } from '@/utils/auth';
import { setCookie } from "@/utils/utils.js";

const state = {
  token: getToken(),
  id: getUserId(),
  name: '',
  flowsId: '',
  roles: ['admin']
}

const getters = {
  getFlowsId(state) { 
		return state.flowsId
	},
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERID: (state, id) => {
    state.id = id
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_FLOWSID: (state, flowsId) => {
    state.flowsId = flowsId
  },
}

const actions = {
  // 账号密码登录
  login({ commit }, userInfo) {
    const { username, password, } = userInfo;
    return processLoginData(login2, { username: username.trim(), password, type: 1}, commit)
  },
  // 微信登录
  wxlogin({ commit }, data) {
    return processLoginData(wechatLogin, { ...data, type: 1}, commit)
  },
  logout({ commit }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      commit('SET_USERID', '')
      removeToken()
      removePerMission()
      resolve()
    })
  },

  setFlowsId({commit}, id) { 
		commit('SET_FLOWSID', id);
	},

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      commit('SET_USERID', '')
      removeToken()
      resolve()
    })
  },
}

function processLoginData(method, data, commit) {
  return new Promise((resolve, reject) => {
    method(data).then(response => {
      if (response.code === 1001) {
        const { data } = response;
        setCookie('accessToken', data.accessToken, 1000 * 60 * 60); //设置Session
        commit('SET_TOKEN', data.accessToken);
        commit('SET_USERID', data.userId);
        setToken(data.accessToken);
        setUserId(data.userId);
        if (data.adminStatus == 1) {
          data.companyId = null
        }
        commit('SET_NAME', data.userRealName);
        setUserInfo(data);

        /**
         * @Description: 获取权限
         * @param {*}
         * @Author: wangwangwang
         */          
        getPermission().then(res => {
          if (res.code === 1001) {
            /**
             * @Description: 存储用户信息
             * @Param: 
             * @Author: wangwangwang
             */
            setPerMission(res.data)
            getSystemDetails({code: 'watermark'}).then(ress => {
              if (ress.code === 1001) {
                window.sessionStorage.setItem('watermark', ress.data.paramValue);
              }
            })
            resolve();
          } else {
            reject();
          }
        })
      }
      resolve(response);
    }).catch(error => {
      reject(error);
    })
  })
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
